var gUserInfo = JSON.parse(sessionStorage.wsUserLoginResult),
    wsUrl = "ws://localhost:8080/websocket",
    lockReconnect = false;
$(function () {
    if (sessionStorage.wsUserLoginResult) {
        windowList.friendList = JSON.parse(sessionStorage.friendList);
        windowList.friendList.sort(function (a, b) {
            return b.isOnline - a.isOnline
        });
        $.each(windowList.friendList, function (index, userDto) {
            $.showFriendList(userDto);
        });
        createWebSocket(wsUrl);
    } else {
        window.location.href = "../login.html";
    }

    $.initWindow();
    $("#userIcon").on("click", function () {
        $("#userInfoModal").show();
    });

    $(".x_modal_close").on("click", function () {
        $(this).parents(".x_modal_background").hide();
    });

    $("#okBtn").on("click", function () {
        $.sendMsgToServer();
    });


});

$.initWindow = function () {
    $(".list_window .active_horn").show();
    $(".user_info").show();
    var rgb = gUserInfo.userIcon.replace(/\../g, "");
    $("#userNickname").html(gUserInfo.userNickname);
    $("#userIcon").css("background-color", "rgb(" + rgb + ")");
    $("#" + gUserInfo.userAccount + "").hide();
};

$.sendMsgToServer = function () {
    var msg = new MessageModel(),
        type, content = $("#textarea").val();
    if (currentUser == "group")
        type = "group";
    else
        type = "p2p";
    msg.setUserAccount(gUserInfo.userAccount)
        .setUserIcon(gUserInfo.userIcon)
        .setuserNickName(gUserInfo.userNickname)
        .setContent(content)
        .setType(type).setUserTarget(currentUser);
    if (!windowList[currentUser])
        windowList[currentUser] = [];
    windowList[currentUser].push(msg);
    ws.send(JSON.stringify(msg));
    $.sendMsg(gUserInfo.userIcon, content, "right");
    $("#textarea").val("");
};